package com.javainterface.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * 调用永中在线文件预览接口
 */
@Service
public class FileReadService {

    @Autowired
    RestTemplate restTemplate;

    public String fileReadServer(String downloadUrl){
        int convertType=2;
        String extendName[]=downloadUrl.split("\\.");
        int i=extendName.length-1;
        if (extendName[i].equals("pdf"))
            convertType=20;
        else if(extendName[i].equals("doc")||extendName[i].equals("docx"))
            convertType=1;
        else if(extendName[i].equals("ppt")||extendName[i].equals("pptx"))
            convertType=0;
        else if(extendName[i].equals("xls")||extendName[i].equals("xlsx"))
            convertType=1;
        String url="http://mh.behc.com.cn/dcs.web/onlinefile";
        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/x-www-form-urlencoded");
        //添加参数
        MultiValueMap<String,Object> params=new LinkedMultiValueMap<>();
        params.add("downloadUrl",downloadUrl);
        params.add("convertType",convertType);
        System.out.println(params);
        //九九归一
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(params, headers);

        String returnJson=restTemplate.postForObject(url,httpEntity,String.class);
        return returnJson;
    }
}
